<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Course;
use Illuminate\Support\Facades\DB;
class CoursesSection extends Model
{
    protected $table = 'c_courses_section';


    public function Courses()
    {
        return $this->belongsTo('App\Models\Course','courses_id');
    }

    public function doAddCoursesSection($data)
    {
        try {
            DB::beginTransaction(); // 开始事务
//                更新
            if (isset($data['id']) && !empty($data['id'])) {
                $redata = $this->getCourseSectionInfo($data['id']);
                if(empty($redata)){
                    return '该章节不存在!';
                }
                $redata->courses_id = isset($data['courses_id']) ? $data['courses_id'] : $redata->courses_id;
                $redata->courses_video = isset($data['courses_video']) ? $data['courses_video'] : $redata->courses_video;
                $redata->courses_title = isset($data['courses_title']) ? $data['courses_title'] : $redata->courses_title;
                $redata->user_id = isset($data['user_id']) ? $data['user_id'] : $redata->user_id;
                $res = $redata->save();
            } else {
                //添加
                $this->courses_id = $data['courses_id'];
                $this->courses_video = $data['courses_video'];
                $this->courses_title = $data['courses_title'];
                $this->user_id = isset($data['user_id']) ? $data['user_id'] : 0;
                $res = $this->save();
                (new Course())->where([['id','=',$data['courses_id']]])->increment("nums");
            }
            if ($res) {
                DB::commit(); // 提交事务
                return ['section_id' => isset($data['id']) ? $data['id'] : $this->id];
            } else {
                return '添加失败';
                DB::rollback(); // 回滚事务
            }
        } catch (Exception $e) {
            DB::rollback(); // 回滚事务
        }
    }


    public function getCourseSectionInfo($course_id)
    {
        return $this->where("id", '=', $course_id)->first();
    }

    /****
     * @param $result
     * @param $course_id
     * @return array|string
     *
     * IOS批量添加章节
     */
    public function doAddCoursesSectionIos($result, $course_id)
    {
        try {
            DB::beginTransaction(); // 开始事务
            $num = 0;
            $data = [];
            $section = json_decode($result['section'],true);
            foreach ($section as $k => $v) {
                $data[$k]['courses_id'] = $course_id;
                $data[$k]['courses_video'] = $v['courses_video'];
                $data[$k]['courses_title'] = $v['courses_title'];
                $data[$k]['user_id'] = $v['user_id'];
                $data[$k]['created_at'] = date("Y-m-d H:i:s", time());
                $data[$k]['updated_at'] = date("Y-m-d H:i:s", time());
                $num++;
            }
            $res = DB::table('c_courses_section')->insert($data);
            (new Course())->where([['id', '=', $course_id]])->update(['nums' => $num]);
            if ($res) {
                DB::commit(); // 提交事务
                return ['section_id' => $res];
            } else {
                return '添加失败';
                DB::rollback(); // 回滚事务
            }
        } catch (Exception $e) {
            DB::rollback(); // 回滚事务
        }
    }

}
